package com.net.xpay.core.service.impl.auto;

import com.net.xpay.common.domain.User;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.validator.AutoAccountValidator;
import com.net.common.exception.BusinessException;
import com.net.xpay.core.domain.auto.AutoPayReceiptAccount;
import com.net.xpay.core.manager.auto.AutoPayReceiptAccountManager;
import com.net.xpay.core.model.AutoPayReceiptAccountUpdateModel;
import com.net.xpay.core.service.auto.AutoPayReceiptAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 86188
 */
@Service
@Slf4j
public class AutoPayReceiptAccountServiceImpl implements AutoPayReceiptAccountService {
    @Autowired
    private AutoPayReceiptAccountManager autoPayReceiptAccountManager;
    @Autowired
    private AutoAccountValidator autoAccountValidator;

    @Override
    public void updateAutoPayAccountInfo(User merchant, AutoPayReceiptAccountUpdateModel model) {
        AutoPayReceiptAccount batch = autoPayReceiptAccountManager.findById(merchant.getId(), model.getId());
        autoAccountValidator.validateUpdateAutoPayAccount(merchant,model);
        batch.setName(model.getName().trim());
        batch.setIdentity(model.getIdentity().trim());
        batch.setRemark(model.getRemark());
        autoPayReceiptAccountManager.updateByPrimaryKeyWithVersion(batch);
    }

    @Override
    public void deleteAutoPayAccount(User merchant, Long id) {
        if(id == null){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "删除失败");
        }
        AutoPayReceiptAccount batch = autoPayReceiptAccountManager.findById(merchant.getId(), id);
        batch.setIsDeleted(1);
        autoPayReceiptAccountManager.updateByPrimaryKeyWithVersion(batch);
    }


}
